/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.piza.toolcenter.operation;

import com.piza.toolcenter.model.DBInfo;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author hong
 */
public class DBManager {
    
    private DBInfo dbInfo;
    
    public DBManager(DBInfo dbInfo){
        this.dbInfo=dbInfo;
    }
    
    
    
    public List<String> getDBTables(){
        List<String> allTables=new ArrayList<String>();
        
        try{
            
            Connection conn=this.getConn();
        String sql="SELECT TABLE_NAME,TABLE_ROWS FROM INFORMATION_SCHEMA.TABLES "
                + "WHERE TABLE_SCHEMA='"+OperationTree.getSchema(dbInfo.getConnectUrl()) +"'";
        PreparedStatement preState=conn.prepareStatement(sql);
        preState.executeQuery();
        
        ResultSet rs=preState.getResultSet();
        while(rs.next()){
            allTables.add(rs.getString(1));
        }
        
        
        }catch(Exception e){
            e.printStackTrace();
        }
        
        
        return allTables;
    }
    
    public Connection getConn(){
        Connection conn=null;
        try{
        Class.forName(dbInfo.getDriverClass());
        conn = DriverManager.getConnection(dbInfo.getConnectUrl(),dbInfo.getUserName(),dbInfo.getPassword());
          
         }catch(Exception e){
             e.printStackTrace();
               return null;
           }
        return conn;
    }
    
}
